package model.environment;

import java.util.Date;
import java.util.List;

import model.Shop;
import model.XML;

public class Shops {
	private static List<Shop> shops = null;
	private static long lastUpdate = new Date().getTime();
	
	/**
	 * Gets a list of shops from the cloud.
	 * @return A list of shops.
	 */
	public static List<Shop> getShops()
	{
		long timeSinceLastUpdate = new Date().getTime() - lastUpdate;
		
		if (shops == null || timeSinceLastUpdate > StaticInfo.UPDATE_TIMEOUT)
		{
			try {
				shops = XML.getShops();
				lastUpdate = new Date().getTime();
			}
			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return shops;
	}
	
	/**
	 * Gets a shop from the cloud.
	 * @param id The shop's id.
	 * @return A shop with the specified id.
	 */
	public static Shop getShop(int id) {
		Shop result = null;
			
		List<Shop> shops = getShops();
		
		for (Shop s : shops) {
			if (s.getShopID() == id) {
				result = s;
				break;
			}
		}
		
		return result;
	}
	
	/**
	 * Forces the shop list to update the next time getShops() is called.
	 */
	public static void resetShopList() {
		shops = null;
	}
}
